//
//  LGKNoteMainCell.m
//  LGKNote
//
//  Created by 刘亚军 on 2023/5/26.
//

#import "LGKNoteMainCell.h"
#import "LGKConst.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "LGKNoteMainModel.h"

#define LGKNoteMainMargin 10
#define LGKNoteMainPadding 10
@interface LGKNoteMainImgItem ()
@property (nonatomic,strong) UIImageView *imgView;
@end
@implementation LGKNoteMainImgItem
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]){
        self.imgView = [[UIImageView alloc] initWithFrame:CGRectZero];
        self.imgView.contentMode = UIViewContentModeScaleAspectFill;
        [self.imgView yj_clipLayerWithRadius:5 width:0 color:nil];
        [self.contentView addSubview:self.imgView];
        [self.imgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.contentView);
        }];
    }
    return self;
}
- (void)setImgUrl:(NSString *)imgUrl{
    _imgUrl = imgUrl;
    [self.imgView sd_setImageWithURL:[NSURL URLWithString:[imgUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]] placeholderImage:lgk_getColorImg(LG_ColorWithHex(0xe6e6e6), CGSizeMake(LG_ScreenWidth, LG_ScreenWidth))];
}
@end


@interface LGKNoteMainCell ()

@end
@implementation LGKNoteMainCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.backgroundColor = UIColor.clearColor;
        self.contentView.backgroundColor = UIColor.clearColor;
        [self.contentView addSubview:self.bgView];
        [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.contentView);
            make.left.equalTo(self.contentView).offset(LGKNoteMainMargin);
            make.top.equalTo(self.contentView).offset(6);
            make.bottom.equalTo(self.contentView);
        }];
        [self.bgView addSubview:self.keyPointImgView];
        [self.keyPointImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bgView);
            make.right.equalTo(self.bgView).offset(-8);
            make.width.height.mas_equalTo(28);
        }];
        [self.bgView addSubview:self.titleLab];
        [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bgView).offset(LGKNoteMainPadding);
            make.top.equalTo(self.bgView).offset(LGKNoteMainPadding);
            make.right.equalTo(self.keyPointImgView.mas_left).offset(-15);
            make.height.mas_equalTo(25);
        }];
        
        [self.bgView addSubview:self.infoLab];
        [self.infoLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.titleLab);
            make.bottom.equalTo(self.bgView).offset(-LGKNoteMainPadding);
            make.centerX.equalTo(self.bgView);
            make.height.mas_equalTo(18);
        }];
        [self layoutMidUI];
    }
    return self;
}
- (void)layoutMidUI{
    
}
- (void)setMainModel:(LGKNoteMainModel *)mainModel{
    _mainModel = mainModel;
    NSMutableAttributedString *titleAttr = mainModel.NoteTitle.yj_toMutableAttributedString;
    [titleAttr yj_setCustomFont:LG_SysFont(17)];
    [titleAttr yj_setColor:LG_ColorWithHex(0x252525)];
    if (!IsStrEmpty(self.searchKey) && [mainModel.NoteTitle.lowercaseString containsString:self.searchKey.lowercaseString]) {
        NSRange range = [mainModel.NoteTitle.lowercaseString rangeOfString:self.searchKey.lowercaseString];
        [titleAttr yj_setColor:UIColor.orangeColor atRange:range];
    }
    self.titleLab.attributedText = titleAttr;
    self.keyPointImgView.hidden = !mainModel.IsKeyPoint;
    
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] init];
    CGFloat imgHeight = 13;
    
    NSTextAttachment *attachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
    attachment.image = [UIImage yj_imageNamed:@"note_time" atBundle:LGKNoteManager.defaultManager.noteBundle];
    attachment.bounds = CGRectMake(0, 0, imgHeight, imgHeight);
    NSMutableAttributedString *imgAttr = [NSAttributedString attributedStringWithAttachment:attachment].mutableCopy;
    [imgAttr addAttributes:@{NSBaselineOffsetAttributeName:@(-1)} range:NSMakeRange(0, imgAttr.length)];
    
    NSMutableAttributedString *timeAttr = [@" " stringByAppendingString:[mainModel.UpdateTime.yj_absoluteDateTimeString stringByAppendingString:@"      "]].yj_toMutableAttributedString;
    [timeAttr yj_setFont:13];
    [timeAttr yj_setColor:LG_ColorWithHex(0x999999)];
    
    
    NSTextAttachment *sAttachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
    sAttachment.image = [UIImage yj_imageNamed:@"note_link" atBundle:LGKNoteManager.defaultManager.noteBundle];
    sAttachment.bounds = CGRectMake(0, 0, imgHeight, imgHeight);
    NSMutableAttributedString *sImgAttr = [NSAttributedString attributedStringWithAttachment:sAttachment].mutableCopy;
    [sImgAttr addAttributes:@{NSBaselineOffsetAttributeName:@(-1)} range:NSMakeRange(0, sImgAttr.length)];
    
    
    NSMutableAttributedString *sourceAttr = [@" " stringByAppendingString:mainModel.SysName].yj_toMutableAttributedString;
    [sourceAttr yj_setFont:13];
    [sourceAttr yj_setColor:LG_ColorWithHex(0x999999)];
    
    
    [attr appendAttributedString:imgAttr];
    [attr appendAttributedString:timeAttr];
    [attr appendAttributedString:sImgAttr];
    [attr appendAttributedString:sourceAttr];
    self.infoLab.attributedText = attr;
}
#pragma mark - lazy
- (UIView *)bgView{
    if (!_bgView){
        _bgView = [UIView new];
        _bgView.backgroundColor = UIColor.whiteColor;
        [_bgView yj_clipLayerWithRadius:6 width:0 color:nil];
    }
    return _bgView;
}
- (UILabel *)titleLab{
    if (!_titleLab){
        _titleLab = UILabel.new;
    }
    return _titleLab;
}
- (UILabel *)textLab{
    if (!_textLab){
        _textLab = UILabel.new;
        _textLab.font = LG_SysFont(14);
        _textLab.textColor = LG_ColorWithHex(0x444444);
        _textLab.numberOfLines = 5;
    }
    return _textLab;
}
- (UILabel *)infoLab{
    if (!_infoLab){
        _infoLab = UILabel.new;
    }
    return _infoLab;
}
- (UIImageView *)noteImgView{
    if (!_noteImgView){
        _noteImgView = [[UIImageView alloc] initWithFrame:CGRectZero];
        _noteImgView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _noteImgView;
}
- (UIImageView *)keyPointImgView{
    if (!_keyPointImgView){
        _keyPointImgView = [[UIImageView alloc] initWithImage:[UIImage yj_imageNamed:@"note_remark_selected" atBundle:LGKNoteManager.defaultManager.noteBundle]];
    }
    return _keyPointImgView;
}

@end


@implementation LGKNoteMainTextImgCell
- (void)layoutMidUI{
    [self.bgView addSubview:self.noteImgView];
    [self.noteImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.infoLab);
        make.bottom.equalTo(self.infoLab.mas_top).offset(-8);
        make.top.equalTo(self.titleLab.mas_bottom).offset(8);
        make.height.mas_equalTo(100);
        make.width.equalTo(self.noteImgView.mas_height).multipliedBy(1.25);
    }];
    [self.noteImgView yj_clipLayerWithRadius:5 width:0 color:nil];
    [self.bgView addSubview:self.textLab];
    [self.textLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.noteImgView.mas_left).offset(-10);
        make.bottom.top.equalTo(self.noteImgView);
        make.left.equalTo(self.infoLab);
    }];
}
- (void)setMainModel:(LGKNoteMainModel *)mainModel{
    [super setMainModel:mainModel];
    self.textLab.text = mainModel.filterStr;
    [self.noteImgView sd_setImageWithURL:[NSURL URLWithString:[mainModel.imgUrlArr.firstObject stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]] placeholderImage:lgk_getColorImg(LG_ColorWithHex(0xe6e6e6), CGSizeMake(LG_ScreenWidth, LG_ScreenWidth))];
}
@end


@interface LGKNoteMainImgCell ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,assign) CGFloat itemWidth;
@end
@implementation LGKNoteMainImgCell
- (void)layoutMidUI{
    self.itemWidth = (LG_ScreenWidth - LGKNoteMainMargin*2 - LGKNoteMainPadding*2 - 10*2)/3;
    [self.bgView addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.infoLab);
        make.bottom.equalTo(self.infoLab.mas_top).offset(-8);
        make.top.equalTo(self.titleLab.mas_bottom).offset(8);
        make.height.mas_equalTo(self.itemWidth * 0.8);
    }];
}
- (void)setMainModel:(LGKNoteMainModel *)mainModel{
    [super setMainModel:mainModel];
    [self.collectionView reloadData];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return MIN(3, self.mainModel.imgUrlArr.count);
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    LGKNoteMainImgItem *item = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(LGKNoteMainImgItem.class) forIndexPath:indexPath];
    item.imgUrl = self.mainModel.imgUrlArr[indexPath.item];
    return item;
}
- (UICollectionView *)collectionView{
    if (!_collectionView){
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 0;
        if (IsIPad) {
            layout.minimumInteritemSpacing = 10;
            layout.itemSize = CGSizeMake(100, 100);
        }else{
            layout.minimumInteritemSpacing = 10;
            CGFloat width = self.itemWidth;
            layout.itemSize = CGSizeMake(width, width*0.8);
        }
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.userInteractionEnabled = NO;
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        [_collectionView registerClass:[LGKNoteMainImgItem class] forCellWithReuseIdentifier:NSStringFromClass([LGKNoteMainImgItem class])];
    }
    return _collectionView;
}
@end


@implementation LGKNoteMainTextCell
- (void)layoutMidUI{
    [self.bgView addSubview:self.textLab];
    [self.textLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.infoLab.mas_top).offset(-8);
        make.top.equalTo(self.titleLab.mas_bottom).offset(8);
        make.left.right.equalTo(self.infoLab);
    }];
}
- (void)setMainModel:(LGKNoteMainModel *)mainModel{
    [super setMainModel:mainModel];
    self.textLab.text = mainModel.filterStr;
}
@end
